import {
    userInfoStorage
} from '@/common/utils/config.js'

// 白名单
const whiteList = [
  '/', // 注意入口页必须直接写 '/'
  '/pages/home/home',
  '/pages/sclList/sclList',
  '../home/home',
  '/subcontractorA/authorization/authorization',
  '/subcontractorA/userPrivacyPolicy/userPrivacyPolicy',
  '/subcontractorA/payment/details',
  '/subcontractorC/pages/sclIntroduce/sclIntroduce',
  '/subcontractorC/pages/courseArticle/courseArticle',
  '/subcontractorC/pages/courseVideo/courseVideo',
  '/subcontractorD/pages/sclDoQuestion/sclDoQuestion',
  '/subcontractorA/search/search',
  '/pages/course/course',
  '/subcontractorA/consultingTypeList/consultingTypeList',
  '../../subcontractorA/scheduling/scheduling',
  '../consultantDetails/consultantDetails',
  '/subcontractorA/consultantDetails/consultantDetails',
  '/pages/orderList/orderList',
  '/pages/me/me',
  '../../subcontractorA/consultantDetails/consultantDetails',
  '/subcontractorA/scheduling/scheduling',
  '/subcontractorC/pages/sclSearch/sclSearch',
  '../consultingTypeList/consultingTypeList',
  // '/subcontractorA/wxLogin/wxLogin',
  
]
// '/subcontractorB/pages/exchangeGoods/exchangeGoods'

export default async function() {
  const list = ['navigateTo', 'redirectTo', 'reLaunch', 'switchTab']
  // 用遍历的方式分别为,uni.navigateTo,uni.redirectTo,uni.reLaunch,uni.switchTab这4个路由方法添加拦截器
  list.forEach(item => {
    uni.addInterceptor(item, {
      invoke(e) {
        // 获取要跳转的页面路径（url去掉"?"和"?"后的参数）
        const url = e.url.split('?')[0]
        console.log('url', url)

        // 判断当前窗口是白名单，如果是则不重定向路由
        let pass
        if (whiteList) {
          pass = whiteList.some((item) => {
            if (typeof (item) === 'object' && item.pattern) {
              return item.pattern.test(url)
            }
            return url === item
          })
        }
        
        // 不是白名单并且没有token
        if (!pass && !uni.getStorageSync('ttToken')) {
          setTimeout(()=>{
            uni.showToast({
              title: '请先登录后,再进行操作',
              icon: 'none'
            })
          },1000)
        
          uni.navigateTo({
            url: "/subcontractorA/authorization/authorization"
          })
          
          return false
        }
        return e
      },
      fail(err) { // 失败回调拦截
        console.log(err)
      }
    })
  })
}